<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/20 0020
 * Time: 下午 3:43
 */

namespace App\Model\Backstage;

use App\Model\Base;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class Staffattendance extends Base
{
    //公司上班时间配置表
    protected $table;
    //员工考勤表
    protected $staff_attendance_table;
    protected $department_table;
    public $timestamps = false;

    public function __construct()
    {
        parent::__construct();
        //公司上班时间配置表
        $this->table = 'work_time';
//        $this->staff_attendance_table = 'staff_attendance_'.$this->branch_id;
        $this->staff_attendance_table = 'staff_attendance';
//        $this->department_table = 'department_'.$this->branch_id;
        $this->department_table = 'department';
    }


    /** 上班打卡操作数据库
     * @param $dayTime
     * @return array
     */
    public  function gotoWork($dayTime)
    {
        $user = Auth::user();
        // 查询当前用户公司上班时间
        $workTime = DB::table($this->table)->where('branch_id', $user->u_b_id)->value('to_work_time');
        $is_Work = DB::table($this->staff_attendance_table)->where('employee_id', $user->u_id)
            ->where('date', $dayTime)->value('start_work_time');
        $second=floor(time()-(strtotime(date('Y-m-d ').$workTime)));
        if ($is_Work){
            return ['code' => 201 , 'msg' => '今天已经上班打卡'];
        }
        // 计算迟到时间
        if ($second > 0){
            // 迟到
            $res = DB::table($this->staff_attendance_table)
                ->insertGetId([
                    'date' => $dayTime,
                    'employee_id' => $user->u_id,
                    's_d_id'      => $this->store_id,
                    'start_work_time' => time(),
                    'check_type' => 2,
                    'late_time' => $second,
                ]);
        }else{
            //正常上班
            $res = DB::table($this->staff_attendance_table)
                ->insertGetId([
                    'date' => $dayTime,
                    's_d_id'     => $this->store_id,
                    'employee_id' => $user->u_id,
                    'start_work_time' => time(),
                    'check_type' => 1,
                    'late_time' => 0,
                ]);
        }
        if ($res){
            return ['code' => 200, 'msg' => '打卡成功'];
        }else{
            return ['code' => 205, 'msg' => '打卡失败'];
        }
    }

    /** 下班打卡操作数据库
     * @param $dayTime
     * @return array
     */
    public  function afterWork($dayTime, $request)
    {
        $user = Auth::user();
        // 查询当前用户公司上班时间
        $workTime = DB::table($this->table)->where('branch_id', $user->u_b_id)->value('after_work_time');
        $is_Work = DB::table($this->staff_attendance_table)->where('employee_id', $user->u_id)
            ->where('date', $dayTime)->value('start_work_time');
        $second=floor(time()-(strtotime(date('Y-m-d ').$workTime)));
        if ($second > 0){
            // 正常下班
            if ($is_Work){
                // 有上班打卡  正常下班打卡
                $res = DB::table($this->staff_attendance_table)
                    ->where('employee_id', $user->u_id)
                    ->where('date', $dayTime)
                    ->update([
                        'knock_off_time' => time(),
                        'summary_work' => $request->input('summary_work'),
                        'sign_type'    => $request->input('sign_type')
                    ]);
            }else{
                // 没有上班打卡  正常下班打卡
                $res = DB::table($this->staff_attendance_table)
                    ->insertGetId([
                        'date' => $dayTime,
                        'employee_id' => $user->u_id,
                        'knock_off_time' => time(),
                        'end_type' => 1,
                        'summary_work' => $request->input('summary_work'),
                        'sign_type'    => $request->input('sign_type'),
                        's_d_id'       => $this->store_id
                    ]);
            }
        }else{
            //早退下班
            if ($is_Work){
                // 有上班打卡  早退下班打卡
                $res = DB::table($this->staff_attendance_table)
                    ->where('employee_id', $user->u_id)
                    ->where('date', $dayTime)
                    ->update([
                        'knock_off_time' => time(),
                        'end_type' => 2,
                        'summary_work' => $request->input('summary_work'),
                        'leave_time' => abs($second),
                        'sign_type'    => $request->input('sign_type'),
                    ]);
            }else{
                // 没有上班打卡  早退下班打卡
                $res = DB::table($this->staff_attendance_table)
                    ->insertGetId([
                        'date' => $dayTime,
                        'employee_id' => $user->u_id,
                        'knock_off_time' => time(),
                        'end_type' => 1,
                        'summary_work' => $request->input('summary_work'),
                        'leave_time' => abs($second),
                        'sign_type'    => $request->input('sign_type'),
                        's_d_id'       => $this->store_id
                    ]);
            }
        }
        if ($res){
            return ['code' => 200, 'msg' => '打卡成功'];
        }else{
            return ['code' => 205, 'msg' => '打卡失败'];
        }
    }

    /** 根据时间查询打卡详情
     * @param $startTime
     * @param $endTime
     */
    public function staffRankings($map, $pageLimit)
    {

        $field = ['date', DB::raw('FROM_UNIXTIME(start_work_time) as start_work_time'), DB::raw('FROM_UNIXTIME(knock_off_time) as knock_off_time'),
                   DB::raw('FROM_UNIXTIME(late_time) as late_time'),DB::raw('FROM_UNIXTIME(leave_time) as leave_time'), 'end_type', 'check_type',
                'u_id', 'u_username',
                'name'
        ];
        $id = $this->staff_attendance_table.'.id';
        $list = DB::table($this->staff_attendance_table)
            ->leftjoin('user', 'employee_id', '=', 'u_id')
            ->leftJoin('department', 's_d_id', '=', 'department.id')
            ->where('user.is_del',0)
            ->where($map)
            ->select($field)
            ->orderBy($id, 'desc')
            ->paginate($pageLimit);
        return $list;
    }

    /**
     * @param $id
     * @param $summary_work
     * @return mixed
     * 签到经理点评
     */
    public function siginComment($id, $comments){
        return DB::table($this->staff_attendance_table)
            ->where('id', $id)
            ->update(['comments' => $comments]);
    }
}